Example analysis of C++ timestamp conversion operation [involving time zone conversion of GMT and CST]

  • 2020-05-19 05:24:11
  • OfStack

This article illustrates the C++ timestamp conversion operation as an example. I will share it with you for your reference as follows:

Question origin

Timestamp conversion (timestamp: total number of seconds from January 1, 1970 (00:00:00) to the current time).


#include <stdio.h>
#include <time.h>
int main(int argc, const char * argv[])
{
  time_t t;
  struct tm *p;
  t=1408413451;
  p=gmtime(&t);
  char s[80];
  strftime(s, 80, "%Y-%m-%d %H:%M:%S", p);
  printf("%d: %s\n", (int)t, s);
}

The results of


1408413451   2014-08-19 01:57:1408384651

However, the result calculated using the command at the linux terminal is not 1


[###t]$ date -d @1408413451
Tue Aug 19 09:57:31 CST 2014

By comparison, it is found that the two are exactly 8 hours apart. CST stands for Greenwich time, and the time zone can be output by strftime () function, which can be corrected as follows


#include <stdio.h>
#include <time.h>
int main(int argc, const char * argv[])
{
  time_t t;
  struct tm *p;
  t=1408413451;
  p=gmtime(&t);
  char s[80];
  strftime(s, 80, "%Y-%m-%d %H:%M:%S::%Z", p);
  printf("%d: %s\n", (int)t, s);
}

The results of


1408413451: 2014-08-19 01:57:31::GMT

Dig into

GMT(Greenwich Mean Time) stands for Greenwich mean time. In the 107th century, the royal observatory of Greenwich conducted celestial observations for the purposes of an expansion of maritime supremacy. The old royal observatory was founded in 1675, using Greenwich's meridian as the zero degree of longitude dividing the eastern and western hemispheres of the earth. Observatory door wall there is a 24 hour clock, display the current time, for the world, this time is set by the world time reference point, the world as a standard to set the time in Greenwich time, this is our familiar "GMT" (Greenwich Mean Time, hereinafter referred to as G. M. T.).

CST can represent four different time zones at the same time:


Central Standard Time (USA) UT-6:00
Central Standard Time (Australia) UT+9:30
China Standard Time UT+8:00
Cuba Standard Time UT-4:00

It can be seen that CST can simultaneously represent the standard time of the United States, Australia, China and Cuba.

Well, the difference is 8 hours (CST is 8 hours later/larger than GMT), GMT+8*3600=CST, the code is as follows


#include <stdio.h>
#include <time.h>
int main(int argc, const char * argv[])
{
  time_t t;
  struct tm *p;
  t=1408413451;
  p=gmtime(&t);
  char s[80];
  strftime(s, 80, "%Y-%m-%d %H:%M:%S::%Z", p);
  printf("%d: %s\n", (int)t, s);
  t=1408413451 + 28800;
  p=gmtime(&t);
  strftime(s, 80, "%Y-%m-%d %H:%M:%S", p);
  printf("%d: %s\n", (int)t, s);
  return 0;
}

The results of


1408413451: 2014-08-19 01:57:31::GMT
1408442251: 2014-08-19 09:57:31

linux platform


Tue Aug 19 09:57:31 CST 2014

PS: this site also provides an Unix timestamp conversion tool, including various common languages for timestamp operation methods, for your reference:

Unix timestamp (timestamp) conversion tool:
http://tools.ofstack.com/code/unixtime

I hope this article is helpful to you C++ programming.


Related articles: